﻿using DotNetMVC.OA.IBLL;
using DotNetMVC.OA.Model;
using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DotNetMVC.OA.UI.Portal.Controllers
{
    public class BaseController : Controller
    {
        public UserInfo LoginUser { get; set; }
        public bool IsCheckUserLogin = true;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);



            #region old
            /*
    //当前执行的Controller的名字
    string ctrlName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
    //当前执行的Action的名字
    //string actionName = filterContext.ActionDescriptor.ActionName;
    //if (ctrlName == "Login" && (actionName == "Index" || actionName == "Login"))
    if (ctrlName == "UserLogin")
    {
        //什么都不做
        return;
    }

    if (IsCheckUserLogin)
    {
        //校验用户是否已经登录
        if (filterContext.HttpContext.Session["loginUser"] == null)
        {
            filterContext.HttpContext.Response.Redirect("/UserLogin/Index");
        }
        else
        {
            LoginUser = (UserInfo)filterContext.HttpContext.Session["loginUser"];
        }
    }
    /**/
            #endregion


            if (IsCheckUserLogin)
            {
                //使用mm+cookie代替session
                //校验用户是否已经登录

                //从缓存中拿到当前的登录的用户信息。
                if (Request.Cookies["userLoginId"] == null)
                {
                    filterContext.HttpContext.Response.Redirect("/UserLogin/Index");
                    return;
                }
                string userGuid = Request.Cookies["userLoginId"].Value;
                UserInfo userInfo = Common.Cache.CacheHelper.GetCache(userGuid) as UserInfo;
                if (userInfo == null)
                {
                    //用户长时间不操作，。超时。
                    filterContext.HttpContext.Response.Redirect("/UserLogin/Index");
                    return;
                }
                LoginUser = userInfo;
                //滑动窗口机制。
                Common.Cache.CacheHelper.SetCache(userGuid, userInfo, DateTime.Now.AddMinutes(20));

                //校验权限
                if (LoginUser.UName == "admin")
                {
                    return;   //后门
                }

                string url = Request.Url.AbsolutePath.ToLower();
                string httpMethod = Request.HttpMethod.ToLower();

                //通过Spring.Net容器创建对象
                IApplicationContext ctx = ContextRegistry.GetContext();
                IUserInfoService UserInfoService = ctx.GetObject("UserInfoService") as IUserInfoService;
                IActionInfoService ActionInfoService = ctx.GetObject("ActionInfoService") as IActionInfoService;

                IR_UserInfo_ActionInfoService R_UserInfo_ActionInfoService = ctx.GetObject("R_UserInfo_ActionInfoService") as IR_UserInfo_ActionInfoService;
                IR_UserInfo_RoleInfoService R_UserInfo_RoleInfoService = ctx.GetObject("R_UserInfo_RoleInfoService") as IR_UserInfo_RoleInfoService;
                IR_RoleInfo_ActionInfoService R_RoleInfo_ActionInfoService = ctx.GetObject("R_RoleInfo_ActionInfoService") as IR_RoleInfo_ActionInfoService;
                short delFlagNormal = (short)Model.Enum.DelFlagEnum.Normal;

                //获取当前权限
                var actionInfo = ActionInfoService.GetEntities(a => a.Url.ToLower() == url
                                            && a.HttpMethod.ToLower() == httpMethod
                                            && a.DelFlag == delFlagNormal).FirstOrDefault();
                if (actionInfo == null)
                {
                    Response.Redirect("/Error.html");
                    return;
                }

                //用户特殊权限
                var rUA = R_UserInfo_ActionInfoService.GetEntities(r => r.UserInfoID == LoginUser.ID
                                                    && r.ActionInfoID == actionInfo.ID
                                                    && r.DelFlag == delFlagNormal).FirstOrDefault();
                if (rUA != null)
                {
                    if (rUA.HasPermission == true)
                    {
                        return;
                    }
                    else
                    {
                        Response.Redirect("/Error.html");
                        return;
                    }
                }

                //获取用户拥有角色Id
                var allRoleIds = R_UserInfo_RoleInfoService.GetEntities(r => r.UserInfoID == LoginUser.ID).Select(r => r.RoleInfoID).ToList();
                //用户角色是否拥有权限
                var actionCount = R_RoleInfo_ActionInfoService.GetEntities(r => allRoleIds.Contains(r.RoleInfoID)
                                                                && r.ActionInfoID == actionInfo.ID
                                                                && r.DelFlag == delFlagNormal).Count();
                if (actionCount <= 0)
                {
                    Response.Redirect("/Error.html");
                    return;
                }
            }
        }
    }
}